

#include "dev_GT911.h"

//#define GT911_DBG 1


//IIC方式：软件IIC

IICdevice* iic_io;
gpio* rst_io;
gpio* int_io;



///* 定义GT911复位与中断引脚连接的GPIO端口 */
//#define GT911_RST_1()  mcu_set_io(rst_io,1)  /* RST = 1 */
//#define GT911_RST_0()  mcu_set_io(rst_io,0)  /* RST = 0 */
//#define GT911_INT_1()  mcu_set_io(int_io,1)  /* INT = 1 */
//#define GT911_INT_0()  mcu_set_io(int_io,0)  /* INT = 0 */
//#define GT911_INT_in()  mcu_read_io(int_io)  /* INT_IN */
//// 使能中断引脚，设置输入输出状态。
//#define SET_GT911_INT_out()  mcu_set_io_out(int_io)
//#define SET_GT911_INT_in()   mcu_set_io_in(int_io)
//#define SET_GT911_RST_out()  mcu_set_io_out(rst_io)


///* 定义GT911复位与中断引脚连接的GPIO端口 */
//void GT911_RST_1() { mcu_set_io(rst_io,1) ;} /* RST = 1 */
//void GT911_RST_0() { mcu_set_io(rst_io,0) ;} /* RST = 0 */
//void GT911_INT_1() { mcu_set_io(int_io,1) ;} /* INT = 1 */
//void GT911_INT_0() { mcu_set_io(int_io,0) ;} /* INT = 0 */
//unt32 GT911_INT_in() {return  mcu_read_io(int_io) ;} /* INT_IN */
//// 使能中断引脚，设置输入输出状态。
//void SET_GT911_INT_out() { mcu_set_io_out(int_io);}
//void SET_GT911_INT_in()  { mcu_set_io_in(int_io);}
//void SET_GT911_RST_out() { mcu_set_io_out(rst_io);}



/*
*	函 数 名: GT911_IIC_Init
*	功能说明: IIC初始化
*	形 参: 无
*	返 回 值: 1成功 -1失败
*/
int GT911_IIC_Init()
{
	Soft_IIC_Init(iic_io);
	printfs("I2C_Init_OK... \r\n");
	return 1;
}


/*GT911 的从设备地址有两组可选，两组地址分别为：0xBA/0xBB和0x28/0x29*/
//unt8  GT911_I2C_ADDR	= 0xBA;
//#define GT911_I2C_ADDR	0x28
unt8  GT911_I2C_ADDR	= 0;

/**********************************************************************************************************
*	函 数 名: GT911_WriteReg
*	功能说明: 写1个或连续的多个寄存器
*	形 参: _usRegAddr : 寄存器地址
*	_pRegBuf : 寄存器数据缓冲区
*	_ucLen : 数据长度
*	返 回 值: 1成功
**********************************************************************************************************/
int GT911_WriteReg(unsigned short _usRegAddr, unsigned char* _pRegBuf, unsigned char  _ucLen)
{	
	Soft_IIC_write(iic_io, GT911_I2C_ADDR, _usRegAddr,2, _pRegBuf, _ucLen);
	return 1;
}

/**********************************************************************************************************
*	函 数 名: GT911_ReadReg
*	功能说明: 读1个或连续的多个寄存器
*	形 参: _usRegAddr : 寄存器地址
*	_pRegBuf : 寄存器数据缓冲区
*	_ucLen : 数据长度
*	返 回 值: 1成功
**********************************************************************************************************/
int GT911_ReadReg(unsigned short _usRegAddr, unsigned char* _pRegBuf, unsigned char  _ucLen)
{

	Soft_IIC_read(iic_io, GT911_I2C_ADDR, _usRegAddr, 2, _pRegBuf, _ucLen);

	return 1;
}

// GT911 重定义延时函数
void Delay_ms(int x) 
{
	mcu_delay_ms(x);
}

///////////  上面是硬件接口   ///////////////////////////////////////////////////////////


#define GT911_READ_XY_REG 0x814E	/* 坐标寄存器 */
#define GT911_CLEARBUF_REG 0x814E	/* 清除坐标寄存器 */
#define GT911_CONFIG_REG	0x8047	/* 配置参数寄存器 */
#define GT911_COMMAND_REG 0x8040 /* 实时命令 */
#define GT911_PRODUCT_ID_REG 0x8140 /*productid*/
#define GT911_VENDOR_ID_REG 0x814A /* 当前模组选项信息 */
#define GT911_CONFIG_VERSION_REG 0x8047 /* 配置文件版本号 */
#define GT911_CONFIG_CHECKSUM_REG 0x80FF /* 配置文件校验码 */
#define GT911_FIRMWARE_VERSION_REG 0x8144 /* 固件版本号 */



/* GT911单个触点配置参数，一次性写入 */
//7寸 1024x600配置表
unsigned char  s_GT911_CfgParams_1024x600_07[186]=
{
  0x42,0x00,0x04,0x58,0x02,0x0A,0x3D,0x00,0x01,0x08,0x28,0x08,0x5A,0x46,0x03,0x0B,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x10,0x11,0x14,0x0F,0x89,0x2A,0x09,0x4B,0x50,0xB5,0x06,0x00,0x00,0x00,0x00,0x02,0x1D,0x00,0x01,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x78,0x94,0xC5,0x02,0x07,0x00,0x00,0x04,0x90,0x40,0x00,0x81,0x4A,0x00,
	0x75,0x55,0x00,0x6B,0x61,0x00,0x62,0x70,0x00,0x62,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x10,0x12,0x14,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x04,0x06,0x08,0x0A,0x0F,0x10,0x12,0x16,0x18,0x1C,
	0x1D,0x1E,0x1F,0x20,0x21,0x22,0x24,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x01	
};

//4.3寸 800x480配置表
unsigned char  s_GT911_CfgParams_800x480_43[186]=
{
	0x42,0x20,0x03,0xE0,0x01,0x0A,0x3D,0x00,0x02,0x08,0x28,0x08,0x64,0x46,0x03,0x05,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x18,0x1A,0x1E,0x14,0x89,0x2A,0x09,0xC8,0xCA,0x40,0x04,0x00,0x00,0x00,0x61,0x02,0x1D,0x00,0x01,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xA0,0xFA,0x94,0xD5,0xF4,0x07,0x00,0x00,0x04,0x86,0xA7,0x00,0x82,0xB7,0x00,
	0x80,0xC8,0x00,0x7D,0xDA,0x00,0x7C,0xEF,0x00,0x7C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x10,0x12,0x14,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x04,0x06,0x08,0x0A,0x0F,0x10,0x12,0x16,0x18,0x1C,
	0x1D,0x1E,0x1F,0x20,0x21,0x22,0x24,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x54,0x01
};

//4.3寸 480x272配置表  f1c100s 裸机代码默认配置表。在 耀元鸿科技 的触摸屏上会有问题，触摸位置会有偏差。
unsigned char  s_GT911_CfgParams_480x272_43__[186]=
{
	0x42,0xE0,0x01,0x10,0x01,0x0A,0x3D,0x00,0x02,0x08,0x28,0x08,0x64,0x46,0x03,0x05,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x18,0x1A,0x1E,0x14,0x89,0x2A,0x09,0xC8,0xCA,0x40,0x04,0x00,0x00,0x00,0x61,0x02,0x1D,0x00,0x01,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xA0,0xFA,0x94,0xD5,0xF4,0x07,0x00,0x00,0x04,0x86,0xA7,0x00,0x82,0xB7,0x00,
	0x80,0xC8,0x00,0x7D,0xDA,0x00,0x7C,0xEF,0x00,0x7C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x10,0x12,0x14,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x04,0x06,0x08,0x0A,0x0F,0x10,0x12,0x16,0x18,0x1C,
	0x1D,0x1E,0x1F,0x20,0x21,0x22,0x24,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x66,0x01
};

//4.3寸 480x272配置表    耀元鸿科技 给的触摸屏的配置文件
unsigned char  s_GT911_CfgParams_480x272_43[186] =
{
	 0x41,0xE0,0x01,0x10,0x01,0x0A,0x0D,0x20,0x01,0x0A,0x28,0x0F,0x5A,0x3C,0x03,0x05,0x00,0x00,0x00,0x00,0x00,0x00,
	 0x05,0x00,0x00,0x00,0x00,0x8A,0x2A,0x0C,0x32,0x34,0x0C,0x08,0x00,0x00,0x00,0x03,0x02,0x25,0x00,0x00,0x00,0x00,
	 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x28,0x55,0x94,0xC5,0x02,0x07,0x00,0x00,0x04,0x8D,0x2B,0x00,0x80,0x32,0x00,
	 0x75,0x3A,0x00,0x6C,0x43,0x00,0x64,0x4F,0x00,0x64,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	 0x00,0x00,0x18,0x16,0x14,0x12,0x10,0x0E,0x0C,0x0A,0x08,0x06,0x04,0x02,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,
	 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x24,0x22,0x21,0x20,0x1F,0x1E,0x1D,0x1C,0x18,0x16,0x13,0x12,
	 0x10,0x0F,0x0A,0x08,0x06,0x04,0x02,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x6A,0x01
};

// 0x41,0xE0,0x01,0x10,0x01,0x0A,0x0D,0x20,0x01,0x0A,0x28,0x0F,0x5A,0x3C,0x03,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x00,0x8A,0x2A,0x0C,0x32,0x34,0x0C,0x08,0x00,0x00,0x00,0x03,0x02,0x25,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x28,0x55,0x94,0xC5,0x02,0x07,0x00,0x00,0x04,0x8D,0x2B,0x00,0x80,0x32,0x00,0x75,0x3A,0x00,0x6C,0x43,0x00,0x64,0x4F,0x00,0x64,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x16,0x14,0x12,0x10,0x0E,0x0C,0x0A,0x08,0x06,0x04,0x02,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x24,0x22,0x21,0x20,0x1F,0x1E,0x1D,0x1C,0x18,0x16,0x13,0x12,0x10,0x0F,0x0A,0x08,0x06,0x04,0x02,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x6A,0x01



#ifdef TP70_1024x600
	#define s_GT911_CfgParams s_GT911_CfgParams_1024x600_07
#endif

#ifdef TP43_800x480
	#define s_GT911_CfgParams s_GT911_CfgParams_800x480_43
#endif

#ifdef TP43_480x272
	#define s_GT911_CfgParams s_GT911_CfgParams_480x272_43
#endif

#if (!defined TP70_1024x600)&&(!defined TP43_800x480)&&(!defined TP43_480x272)
	unsigned char  s_GT911_CfgParams[186];
#endif

GT911_T g_GT911;

/**********************************************************************************************************
*	函 数 名: GT911_INT_GPIO_Input_Init
*	功能说明: 初始化RST引脚为推挽输出，INT引脚为开漏输出
*	形 参: 无
*	返 回 值: 无
**********************************************************************************************************/
void GT911_RST_INT_GPIO_Init(void)
{
	SET_GT911_INT_out();
	SET_GT911_RST_out();
	GT911_RST_1();
	GT911_INT_1();
}

/**********************************************************************************************************
*	函 数 名: GT911_INT_GPIO_Input_Init
*	功能说明: 设定INT引脚为输入悬空
*	形 参: 无
*	返 回 值: 无
**********************************************************************************************************/
void GT911_INT_GPIO_Input_Init(void)
{
	SET_GT911_INT_in();//INT
}

/**********************************************************************************************************
*	函 数 名: GT911_Reset_Sequence
*	功能说明: G911硬复位操作,RST为低电平时，INT持续为低电平，1ms后RST置为高电平，10ms后INT设置为输入，
*	使GT911地址设定为0xBA/0xBB。
*	形 参: 无
*	返 回 值: 无
**********************************************************************************************************/

void GT911_Reset_Sequence(unsigned char  ucAddr)
{
	int tms = 40;//这个延时为30ms,根据系统速度与延时精度适当调节，延时过短将不能读取系统配置与初始化。
	GT911_RST_INT_GPIO_Init();
	switch (ucAddr)
	{
	case 0xBA:
		GT911_RST_0(); //RST引脚低电平
		GT911_INT_0(); //INT引脚低电平
		Delay_ms(tms); //延时30ms，最短1
		GT911_RST_1(); //RST引脚高电平
		GT911_INT_0(); //INT引脚低电平
		Delay_ms(tms); //延时30ms，最短20
		GT911_INT_0();
		Delay_ms(tms); //延时30ms，最短20
		GT911_INT_1();
		break;

	case 0x28:
		GT911_RST_0(); //RST引脚低电平
		GT911_INT_1(); //INT引脚高电平
		Delay_ms(tms); //延时30ms，最短1
		GT911_RST_1(); //RST引脚高电平
		GT911_INT_1(); //INT引脚高电平
		Delay_ms(tms); //延时30ms，最短20
		GT911_INT_0();
		Delay_ms(tms); //延时30ms，最短20
		GT911_INT_1();
		break;

	default: //缺省为0xBA
		GT911_RST_0(); //RST引脚低电平
		GT911_INT_0(); //INT引脚低电平
		Delay_ms(tms); //延时30ms，最短1
		GT911_RST_1(); //RST引脚高电平
		GT911_INT_0(); //INT引脚低电平
		Delay_ms(tms); //延时30ms，最短20
		GT911_INT_0();
		Delay_ms(tms); //延时30ms，最短20
		GT911_INT_1();
		break;

	}
}

/**********************************************************************************************************
*	函 数 名: GT911_Soft_Reset
*	功能说明: G911软复位操作。
*	形 参: 无
*	返 回 值: 无
**********************************************************************************************************/
void GT911_Soft_Reset(void)
{
	unsigned char  buf[1];

	buf[0] = 0x04;
	GT911_WriteReg(GT911_COMMAND_REG, buf, 1);
	Delay_ms(50);

	buf[0] = 0x01;
	GT911_WriteReg(GT911_COMMAND_REG, buf, 1);	
	Delay_ms(50);
}

/**********************************************************************************************************
*	函 数 名: GT911_Exti_Int
*	功能说明: G911中断引脚初始化。
*	形 参: 无
*	返 回 值: 配置参数版本号
**********************************************************************************************************/
void GT911_Exti_Int(void)
{

}

/**********************************************************************************************************
*	函 数 名: GT911_InitHard
*	功能说明: 配置触摸芯片.
*	形 参: 无
*	返 回 值: 无 1:失败，0：成功
**********************************************************************************************************/
int GT911_InitHard(void)
{
	unsigned char  config_Checksum = 0,i;

	g_GT911.i2c_addr = GT911_I2C_ADDR;
	GT911_Reset_Sequence(g_GT911.i2c_addr);

	/*复位GT911，设定设备地址为0xBA/0xBB*/
	GT911_Soft_Reset(); /*软复位*/

	/*读取配置文件版本，计算校验和*/
	s_GT911_CfgParams[0] =GT911_ReadStatue();
	if((s_GT911_CfgParams[0]!=0)&&(s_GT911_CfgParams[0]!=0xff))
	{
		for(i=0;i<sizeof(s_GT911_CfgParams)-2;i++)
		{
			config_Checksum += s_GT911_CfgParams[i];
		}
		s_GT911_CfgParams[184] = (~config_Checksum)+1;
		///* 发送配置信息参数 */
		GT911_WriteReg(GT911_CONFIG_REG,s_GT911_CfgParams,186);
		//读并打印配置表
	//	unsigned char  rd[186];	
	//	GT911_ReadReg(GT911_CONFIG_REG,rd,186);
	//	for(i=0;i<186;i++)printfs("0x%02X,",rd[i]);
		
		GT911_INT_GPIO_Input_Init(); //设定INT引脚为输入悬空
		GT911_Soft_Reset(); /*软复位*/
		/*初始化校准，等待200ms*/
		Delay_ms(200);
		g_GT911.Enable = 1;
		return 1;
	}
	else
	{
		printfs("ERR: TouchPad_Config_Version err... \n\r");
		return 0;
	}
}




/**********************************************************************************************************
*	函 数 名: GT911_ReadFirmwareVersion
*	功能说明: 获得GT911的芯片固件版本
*	形 参: 无
*	返 回 值: 16位版本号
**********************************************************************************************************/
unsigned short GT911_ReadFirmwareVersion(void)
{
	unsigned char  buf[2];
	GT911_ReadReg(GT911_FIRMWARE_VERSION_REG, buf, 2);
	return ((unsigned short)buf[1] << 8) + buf[0];
}

/**********************************************************************************************************
*	函 数 名: GT911_ReadProductID
*	功能说明: 识别显示模块类别。读取GT911 ProductID。
*	形 参: 无
*	返 回 值: 显示模块类别
**********************************************************************************************************/
unsigned int GT911_ReadProductID(void)
{
	unsigned char  buf[4];
	unsigned int value = 0;
	/* Product_ID*/
	GT911_ReadReg(GT911_PRODUCT_ID_REG, buf, 4);
	value = ((unsigned int)buf[3] << 24) + ((unsigned int)buf[2] << 16) + ((unsigned int)buf[1] << 8) + buf[0];
	return value;
}

/**********************************************************************************************************
*	函 数 名: GT911_ReadStatue
*	功能说明: G911读产品ID与配置参数版本号。
*	形 参: 无
*	返 回 值: 配置参数版本号
**********************************************************************************************************/
unsigned char  GT911_ReadStatue(void)
{
	unsigned char  buf[4];
	GT911_ReadReg(GT911_PRODUCT_ID_REG, (unsigned char*)&buf[0], 3);
	GT911_ReadReg(GT911_CONFIG_VERSION_REG, (unsigned char*)&buf[3], 1);
	printfs("TouchPad_ID:%c,%c,%c\r\nTouchPad_Config_Version:%2x\r\n", buf[0], buf[1], buf[2], buf[3]);
	return buf[3];
}


/**********************************************************************************************************
*	函 数 名: GT911_OnePiontScan
*	功能说明: 读取GT911触摸数据，这里仅读取一个触摸点。
*	形 参: 无
*	返 回 值: 无 返回1：没有触摸点数， 返回0，有一个触摸点数
**********************************************************************************************************/
//extern GUI_PID_STATE State;
int GT911_OnePiontScan(void)
{
	unsigned char  buf[9];
	//	static unsigned char  s_tp_down = 0;	

	buf[8] = 0;
	if (g_GT911.Enable == 0)
	{
		return -1;
	}

	/* 读取寄存器：0x814E R Bufferstatus Large_Detect number of touch points */
	/* 判断是否按下，没有按下，直接退出 */
	if (GT911_ReadReg(GT911_READ_XY_REG, (unsigned char*)buf, 1) < 0)
	{
		printfs("I2C_Read_Error...\r\n");
		return -1;
	}
	//触屏点数
	if ((buf[0] & 0x01) == 0)
	{
		//	if (s_tp_down == 1)
		//	{
		//	/* State.x和State.y的数值无需更新，State是全局变量，保存的就是最近一次的数值 */
		//	s_tp_down = 0;
		//	State.Pressed = 0;
		//	GUI_PID_StoreState(&State);
		//	}
		GT911_ReadReg(GT911_READ_XY_REG + 1, &buf[1], 7);
		GT911_WriteReg(GT911_CLEARBUF_REG, (unsigned char*)&buf[8], 1);
		return 1;
	}

	/* 读取第一个触摸点1 */
	GT911_ReadReg(GT911_READ_XY_REG + 1, &buf[1], 7);
	GT911_WriteReg(GT911_CLEARBUF_REG, (unsigned char*)&buf[8], 1);
	/*
	0x814E R/W Bufferstatus Large_Detect number of touch points
	0x814F R track id
	0x8150 R Point1Xl 触摸点 1，X 坐标低 8 位
	0x8151 R Point1Xh 触摸点 1，X 坐标高 8 位
	0x8152 R Point1Yl 触摸点 1，Y 坐标低 8 位
	0x8153 R Point1Yh 触摸点 1，Y 坐标高 8 位
	0x8154 R Point1 触摸点 1，触摸面积低 8 位
	0x8155 R Point1 触摸点 1，触摸面积高 8 位
	*/

	g_GT911.TouchpointFlag = buf[0];
	g_GT911.Touchkey1trackid = buf[1];
	g_GT911.X1 = ((unsigned short)buf[3] << 8) + buf[2];
	g_GT911.Y1 = ((unsigned short)buf[5] << 8) + buf[4];
	g_GT911.S1 = ((unsigned short)buf[7] << 8) + buf[6];
	/* 检测按下 */
	//if (s_tp_down == 0)
	//{
	//	s_tp_down = 1;
	//	State.x = g_GT911.X1;
	//	State.y = g_GT911.Y1;
	//	State.Pressed = 1;
	//	GUI_PID_StoreState(&State);
	//}
	//else
	//{
	//	State.x = g_GT911.X1;
	//	State.y = g_GT911.Y1;
	//	State.Pressed = 1;
	//	GUI_PID_StoreState(&State);
	//}

#ifdef GT911_DBG
	printfs("X1:%5d ,Y1:%5d ,S1:%3d \r\n", g_GT911.X1, g_GT911.Y1, g_GT911.S1); //串口打印坐标值
#endif

	//	g_GT911.X1=0;
	//	g_GT911.Y1=0;
	//	g_GT911.S1=0;
	return 0;
}


/**********************************************************************************************************
*函 数 名: GT911_Scan
*	功能说明: 读取GT911触摸数据。读取全部的数据。
*	形 参: 无
*	返 回 值: 无
*********************************************************************************************************/
void GT911_Scan(void)
{
	unsigned char  buf[40];
	unsigned char  Clearbuf = 0;
	unsigned char  i;
	//	static unsigned char  s_tp_down = 0;
	if (g_GT911.Enable == 0)
	{
		return;
	}

	GT911_ReadReg(GT911_READ_XY_REG, buf, 1);
	if ((buf[0] & 0x0F) == 0)
	{
		//touch release
		GT911_WriteReg(GT911_CLEARBUF_REG, (unsigned char*)&Clearbuf, 1);
		return;
	}
	GT911_ReadReg(GT911_READ_XY_REG + 1, &buf[1], 39);
	GT911_WriteReg(GT911_CLEARBUF_REG, (unsigned char*)&Clearbuf, 1);
	/*
	0x814E R/W Bufferstatus Large_Detect number of touch points
	0x814F R Point1 track id
	0x8150 R Point1Xl 触摸点 1，X 坐标低 8 位
	0x8151 R Point1Xh 触摸点 1，X 坐标高 8 位
	0x8152 R Point1Yl 触摸点 1，Y 坐标低 8 位
	0x8153 R Point1Yh 触摸点 1，Y 坐标高 8 位
	0x8154 R Point1 触摸点 1，触摸面积低 8 位
	0x8155 R Point1 触摸点 1，触摸面积高 8 位
	0x8157 R Point2 track id
	0x8158 R Point2Xl 触摸点 2，X 坐标低 8 位
	0x8159 R Point2Xh 触摸点 2，X 坐标高 8 位
	0x815A R Point2Yl 触摸点 2，Y 坐标低 8 位
	0x815B R Point2Yh 触摸点 2，Y 坐标高 8 位
	0x815C R Point2 触摸点 2，触摸面积低 8 位
	0x815D R Point2 触摸点 2，触摸面积高 8 位
	0x815F R Point3 track id
	0x8160 R Point3Xl 触摸点 3，X 坐标低 8 位
	0x8161 R Point3Xh 触摸点 3，X 坐标高 8 位
	0x8162 R Point3Yl 触摸点 3，Y 坐标低 8 位
	0x8163 R Point3Yh 触摸点 3，Y 坐标高 8 位
	0x8164 R Point3 触摸点 3，触摸面积低 8 位
	0x8165 R Point3 触摸点 3，触摸面积高 8 位
	0x8167 R Point4 track id
	0x8168 R Point4Xl 触摸点 4，X 坐标低 8 位
	0x8169 R Point4Xh 触摸点 4，X 坐标高 8 位
	0x816A R Point4Yl 触摸点 4，Y 坐标低 8 位
	0x816B R Point4Yh 触摸点 4，Y 坐标高 8 位
	0x816C R Point4 触摸点 4，触摸面积低 8 位
	0x816D R Point4 触摸点 4，触摸面积高 8 位
	0x816F R Point5 track id
	0x8170 R Point5Xl 触摸点 5，X 坐标低 8 位
	0x8171 R Point5Xh 触摸点 5，X 坐标高 8 位
	0x8172 R Point5Yl 触摸点 5，Y 坐标低 8 位
	0x8173 R Point5Yh 触摸点 5，Y 坐标高 8 位
	0x8174 R Point5 触摸点 5，触摸面积低 8 位
	0x8175 R Point5 触摸点 5，触摸面积高 8 位
	*/
	g_GT911.TouchpointFlag = buf[0];

	g_GT911.Touchkey1trackid = buf[1];
	g_GT911.X1 = ((unsigned short)buf[3] << 8) + buf[2];
	g_GT911.Y1 = ((unsigned short)buf[5] << 8) + buf[4];
	g_GT911.S1 = ((unsigned short)buf[7] << 8) + buf[6];

	g_GT911.Touchkey2trackid = buf[9];
	g_GT911.X2 = ((unsigned short)buf[11] << 8) + buf[10];
	g_GT911.Y2 = ((unsigned short)buf[13] << 8) + buf[12];
	g_GT911.S2 = ((unsigned short)buf[15] << 8) + buf[14];

	g_GT911.Touchkey3trackid = buf[17];
	g_GT911.X3 = ((unsigned short)buf[19] << 8) + buf[18];
	g_GT911.Y3 = ((unsigned short)buf[21] << 8) + buf[20];
	g_GT911.S3 = ((unsigned short)buf[23] << 8) + buf[22];

	g_GT911.Touchkey4trackid = buf[25];
	g_GT911.X4 = ((unsigned short)buf[27] << 8) + buf[26];
	g_GT911.Y4 = ((unsigned short)buf[29] << 8) + buf[28];
	g_GT911.S4 = ((unsigned short)buf[31] << 8) + buf[30];

	g_GT911.Touchkey5trackid = buf[33];
	g_GT911.X5 = ((unsigned short)buf[35] << 8) + buf[34];
	g_GT911.Y5 = ((unsigned short)buf[37] << 8) + buf[36];
	g_GT911.S5 = ((unsigned short)buf[39] << 8) + buf[38];

	//if (s_tp_down == 0)
	//{
	//	s_tp_down = 1;
	//	//touch down
	//}
	//else
	//{
	////touch move
	//}
#if 1
	for (i = 0; i < 34; i++)
	{
		printfs("%02X ", buf[i]);
	}
	printfs("\r\n");
	printfs("(%5d,%5d,%3d) ", g_GT911.X1, g_GT911.Y1, g_GT911.S1);
	printfs("(%5d,%5d,%3d) ", g_GT911.X2, g_GT911.Y2, g_GT911.S2);
	printfs("(%5d,%5d,%3d) ", g_GT911.X3, g_GT911.Y3, g_GT911.S3);
	printfs("(%5d,%5d,%3d) ", g_GT911.X4, g_GT911.Y4, g_GT911.S4);
	printfs("(%5d,%5d,%3d) ", g_GT911.X5, g_GT911.Y5, g_GT911.S5);
	printfs("\r\n");
#endif
}

/*
*	函 数 名: GT911_Init
*	功能说明: GT911初始化
*	形 参: 无
*	返 回 值: 1成功 -1失败
*/
int GT911_Init(IICdevice* iic, gpio* i_rst, gpio* i_int)
{
	iic_io = iic;
	rst_io = i_rst;
	int_io = i_int;

// GT911 的 I 2 C 从设备地址有两组，分别为 0xBA/0xBB 和 0x28/0x29。
// 地址由 上电初始化时控制 Reset 和 INT 口状态决定。时序图如下：	https://www.cnblogs.com/gylj/articles/17339214.html
	
	
	printfs("GT911_Init... \r\n");
	
	//统一设置引脚模式
	mcu_set_io_out(i_rst);
	mcu_set_io_out(i_rst);
	mcu_set_io_out(iic_io->scl);
	mcu_set_io_out(iic_io->sda);
	mcu_set_io(i_rst,0);
	mcu_set_io(i_rst,0);
	mcu_set_io(iic_io->scl,0);
	mcu_set_io(iic_io->sda,0);
	Delay_ms(20);  
	
	GT911_IIC_Init();  // 
	
//	GT911_RST_INT_GPIO_Init();
//	GT911_InitHard();

	
/*
	*/	
	unt8 addr[]={0xBA,0x28};
//	unt8 addr[]={0x28,0xBA};  	// 这个电路，rst引脚没有接io口，所以无法判断 上电时 rst的状态，只能遍历试试看了。

	int i;
	for(i=0;i<2;i++){	
		GT911_I2C_ADDR = addr[i];	
		GT911_RST_INT_GPIO_Init();
		bool is_init = GT911_InitHard();
		
		if( is_init  == 1){
			break;
		}
		else{						
			GT911_IIC_Init();  // 这里要输入相关引脚			
		}
		printfs("\r\n %d \r\n",GT911_I2C_ADDR);
		Delay_ms(10);
	}
	if( i == 2 ) 
		printfs("GT911_Init err  ----... \r\n");
	else
		printfs("GT911_Init OK... \r\n");
	
	return 1;
}
#include"gl_.h"
void GT911_Test(IICdevice* iic, gpio* i_rst, gpio* i_int)
{
//	unsigned char buf[256];


	printfs("GT911_Test...\r\n");
	/*InitGT911*/
	GT911_Init(iic, i_rst, i_int);

//	GT911_Init(iic, i_rst, i_int);

//	GT911_ReadReg(0x8100, buf, 1);
//	printfs("A[%x]...\r\n ", buf[0]);
//	GT911_ReadReg(0x8048, buf, 6);
//	printfs("A1[%x]...\r\n ", buf[0]);
//	printfs("A2[%x]...\r\n ", buf[1]);
//	printfs("A3[%x]...\r\n ", buf[2]);
//	printfs("A4[%x]...\r\n ", buf[3]);
//	printfs("A5[%x]...\r\n ", buf[4]);
//	printfs("A6[%x]...\r\n ", buf[5]);
//	printfs("Ver[%x]...\r\n ", GT911_ReadFirmwareVersion());

	while (1)
	{
		
		if (GT911_INT_in() == 0)//低电平查询
		{			
			int XSIZE_PHYS_ = 480;
			int YSIZE_PHYS_ = 272;
	
			if(GT911_OnePiontScan()==0 &&
				g_GT911.X1 >= 0 && g_GT911.X1 <= XSIZE_PHYS_ &&
				g_GT911.Y1 >= 0 && g_GT911.Y1 <= YSIZE_PHYS_
			) {			
			// 实际测试发现坐上的坐标变成右下了，这里对调一下
//				int px = XSIZE_PHYS_ -  g_GT911.X1;   // 
//				int py = YSIZE_PHYS_ -  g_GT911.Y1;   //	
				int px =   g_GT911.X1;   // 
				int py =   g_GT911.Y1;   //	
				
				 graphics_draw_point(px, py, 0xFFFF);
			}
		}
		

	}
}
